// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    quesList:[
      {
        id:1,
        type:1,//0 单选 1多选
        questit:'         每一次练习之后，系统将会根据你的做题效果呈现练习报告，分析你知识点掌握情况。每一次的专题报告，系统将会整合到每一科目里最后系统将会呈现你考研政治选择题整体的预测得分。练习报告助你复习更加全面、及时、有针对性，最终提高你的考研政治分数。',
        chose:[
          {
            qid:1,
            tit:'A',
          },
          {
            qid:2,
            tit:'B'
          },
          {
            qid:3,
            tit:'C'
          },
          {
            qid:4,
            tit:'D'
          },
        ],
        true_ans:['A','C'],
        jiexi:'每一次练习之后，系统将会根据你的做题效果呈现练习报告，分析你知识点掌握情况。每一次的专题报告，系统将会整合到每一科目里最后系统将会呈现你考研政治选择题整体的预测得分。练习报告助你复习更加全面、及时、有针对性，最终提高你的考研政治分数。',
        kaodian:[
          {
            ktype:'考点1',
            kcon:'考点内容'
          },
          {
            ktype:'考点2',
            kcon:'考点内容'
          },
        ],
        wrongreas:[
          {
            wid:1,
            wtit:'错误原因1'
          },
          {
            wid:2,
            wtit:'错误原因2'
          },
        ],
        lastwrong:'上次错误原因'
      },
      {
        id:2,
        type:0,//0 单选 1多选
        questit:'选择以下正确答案',
        chose:[
          {
            qid:1,
            tit:'2A'
          },
          {
            qid:2,
            tit:'2B'
          },
          {
            qid:3,
            tit:'2C'
          },
          {
            qid:4,
            tit:'2D'
          },
        ],
        true_ans:'2B',
        jiexi:'2每一次练习之后，系统将会根据你的做题效果呈现练习报告，分析你知识点掌握情况。每一次的专题报告，系统将会整合到每一科目里最后系统将会呈现你考研政治选择题整体的预测得分。练习报告助你复习更加全面、及时、有针对性，最终提高你的考研政治分数。',
        kaodian:[
          {
            ktype:'考点1',
            kcon:'2考点内容'
          },
          {
            ktype:'考点2',
            kcon:'2考点内容'
          },
        ],
        wrongreas:[
          {
            wid:1,
            wtit:'2错误原因1'
          },
          {
            wid:2,
            wtit:'2错误原因2'
          },
        ],
        lastwrong:'2上次错误原因'
      },
    ],
    current:0,
    currentques:[],
    percent:'',
    showqueslist:"",
    showjiexi:false,
    istijiao:false,//是否提交了题目
    each_chose:-1,//选择的答案，用来给点击的样式
    wrongarr:[],//选择的错误原因数组
  },
  // 事件处理函数
  onLoad() {
    var currentques = this.data.currentques;
    this.data.quesList.forEach((item,index)=>{
      item.answer = [];
      item.chose.forEach((list,index2)=>{
        list.class = "";
      })
      item.wrongreas.forEach((wrong,index3)=>{
        wrong.class = "";
      })
      currentques.push(item)
    })
    this.setData({
      currentques:currentques
    })
    console.log(this.data.quesList[this.data.current])
    this.caclnum()
  },
  caclnum(){
    var total = this.data.currentques.length;
    var current = this.data.current+1;
    if(current<total){
      var num = (total-current)/total*100;
    }else{
      var num = '100';
    }
    console.log('num',num)
    this.setData({
      percent:num
    })
    console.log(this.data.percent)
  },
  choseanswer(e){
    let query = e.currentTarget.dataset['index'];
    if(!this.data.istijiao){
      //还没有提交题目，可以随便选择
      if(this.data.currentques[this.data.current].type == 0){
        //单选
        this.setData({
          each_chose:query
        })
        this.data.currentques.forEach((item,index)=>{
          item.chose.forEach((list,index2)=>{
            list.class = "";
            if(index2 == query){
              console.log(1111)
              list.class = "purpleitem"
            }
          })
        })
        this.setData({
          currentques:this.data.currentques
        })
        console.log(this.data.currentques)
      }else{
        //多选
        var currentques = this.data.currentques[this.data.current];
        // this.data.currentques.forEach((item,index)=>{
          currentques.chose.forEach((list,index2)=>{
            if(index2 == query){
              console.log(1111)
              if(list.class == ""){
                list.class = "purpleitem"
                currentques.answer.push(list.tit);
              }else{
                list.class = "";
                if(currentques.answer.indexOf(list.tit) > -1){
                  //数组里有，则删除
                  var qidx = currentques.answer.findIndex(qitem=>{return qitem == list.tit});
                  console.log('未删除',currentques.answer)
                  console.log('点击的',list.tit)
                  console.log(qidx)
                  currentques.answer.splice(qidx,1)
                  console.log('已删除',currentques.answer)
                }
              }
            }
          })
        // })/*  */
        this.setData({
          currentques:this.data.currentques
        })
      }
      
    }
    
  },
  chosew_reason(e){
    //选择错误原因
    let query = e.currentTarget.dataset['index'];
    let wronglist = [];
    this.data.currentques[this.data.current].wrongreas.forEach((wrong,index3)=>{
      if(query == index3){
        if(wrong.class == ''){
          wrong.class = "wrong_item_act";
          this.data.wrongarr.push(wrong.wid)
        }else{
          wrong.class = "";
          if(this.data.wrongarr.indexOf(wrong.wid) > -1){
            //数组里有，则删除
            var widx = this.data.wrongarr.findIndex(witem=>{return witem == wrong.wid});
            this.data.wrongarr.splice(widx,1)
            console.log(this.data.wrongarr)
            console.log(widx)
          }
        } 
      }  
    })
    this.setData({
      currentques:this.data.currentques
    })
  },
  nextti(){
    var current = this.data.current++;
    this.data.currentques.forEach((item,index)=>{
        item.chose.forEach((list,index2)=>{
          list.class = "";
        })
      })
    this.setData({
      istijiao:false,
      currentques:this.data.currentques,
      current:this.data.current
    })
    this.caclnum()
    console.log(this.data.current)
  },
  tijiao(){
    if(this.data.currentques[this.data.current].type == 0){
      //单选
      this.data.currentques[this.data.current].answer = this.data.currentques[this.data.current].chose[this.data.each_chose].tit;
      if(this.data.currentques[this.data.current].answer != this.data.currentques[this.data.current].true_ans){
        //如果选择的答案是错的，则显示红色
        var current = this.data.current;
        var each_chose = this.data.each_chose;
        this.setData({//用setData改变数组里面的值
          ['currentques['+current+'].chose['+each_chose+'].class']:'reditem'
        })
      }
      //对的答案显示绿色
      this.data.currentques.forEach((item,index)=>{
        item.chose.forEach((list,index2)=>{
          if(list.tit == this.data.currentques[this.data.current].true_ans){
            console.log(1111)
            list.class = "greenitem"
          }
        })
      })
    }else{
      //多选
      var chose = this.data.currentques[this.data.current].chose;//当前题目的选项数组
      var answerarr = this.data.currentques[this.data.current].answer;//用户选择的答案
      var true_answerarr = this.data.currentques[this.data.current].true_ans;//正确的答案
      var chosetit = [];//当前题目的选项数组（仅答案tit）
      var samearr = true_answerarr.filter(function(n) {//筛选出相同的答案，即对的答案有哪些      
        return answerarr.indexOf(n) != -1
      });
      chose.forEach((item,index)=>{
        chosetit.push(item.tit)
      })
      console.log('用户选择的答案',answerarr)
      true_answerarr.forEach((item,index)=>{
        //在答案列表中找到正确答案的下标
        var cidx = chosetit.findIndex(tits=>{return tits == item});
        console.log(cidx)
        //给正确答案添加class
        this.data.currentques[this.data.current].chose[cidx].class = "greenitem";
      })
      
      true_answerarr.forEach((item,index)=>{
        //在答案中找到没有选中的正确答案
        if(answerarr.indexOf(item) == -1){
          var cidx = chosetit.findIndex(tits=>{return tits == item});
          console.log(cidx)
          this.data.currentques[this.data.current].chose[cidx].class = "purpleitem";

        }
        //没有选中的正确答案添加class
      })
      answerarr.forEach((aitem,aindex)=>{
        //找出错误答案
        if(true_answerarr.indexOf(aitem) == -1){
          //在答案列表中找到错误答案的下标
          var cidx = chosetit.findIndex(tits=>{return tits == aitem});
          console.log(cidx)
          //给错误答案添加class
          this.data.currentques[this.data.current].chose[cidx].class = "reditem";
          console.log('答错的题',aitem)
        }
      })
    }
    
    this.setData({
      currentques:this.data.currentques,
      istijiao:true
    })
    console.log(this.data.currentques)
  }
})
